<template>
  <div class="news-list-layout">
    <homeHeader v-if="clientwidth>1080"></homeHeader>
    <header-phone v-else></header-phone>

    <div class="news-header" :style="{backgroundImage:'url('+haderBg+')' }">
      <div class="mask"></div>
      <p class="title webfont-Medium">
        关注互联网业界动态，抓住营销增长新趋势
      </p>
      <p class="sub-title">不能错过每一个实现用户增长的机会！</p>
    </div>

    <div class="news-list-main flex">
      <div v-if="clientwidth>1080" class="main-slide">

        <p
          v-for="(item,index) in newsList"
          :class="index===activeIndex?'active pointer webfont-Medium':'pointer webfont-Medium'"
          @click="changeActiveIndex(index)"
        >
          <span>{{ item.name||'' }}</span>
          <i class="el-icon-arrow-right"></i>
        </p>

      </div>
      <div v-else class="main-slide">
        <ul :style="{width:listWidth}">
          <li
            v-for="(item,index) in newsList"
            :class="index===activeIndex?'active pointer webfont-Medium':'pointer webfont-Medium'"
            @click="changeActiveIndex(index)"
          >
            <span>{{ item.name||'' }}</span>
            <i class="el-icon-arrow-right"></i>
          </li>
        </ul>

      </div>

      <div class="list-box">

        <template v-if="newsList.length">
          <div v-for="item in newsList[activeIndex].list" :key="item.media_id" class="new-box flex">
            <div class="new-img pointer" @click="toDetails(item.media_id)">
              <el-image v-if="item.thumb_url" lazy fit="cover" :src="item.thumb_url" class="img-top">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
              <el-image v-if="item.thumb_url" lazy fit="cover" :src="item.thumb_url" class="img-bg">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>

            </div>

            <div class="new-info " @click="toDetails(item.media_id)">
              <div class="news-title webfont-Medium overflow-ellipsis pointer" :title="item.title">{{ item.title }}</div>
              <div class="news-digest  overflow-ellipsis pointer">{{ item.digest }} </div>
              <div class="new-sec flex">
                <p>
                  <i class="el-icon-date"></i>
                  <span>{{ item.time }}</span>
                </p>
                <p v-if="false">
                  <i class="el-icon-view"></i>
                  <span>3432</span>
                </p>
              </div>
            </div>
            <div v-show="clientwidth<=1080&&activeIndex===0" class="new-type" :style="'color:'+spliceColor(item.typeLabel)+' ;'">{{ spliceName(item.typeLabel) }}</div>

          </div>
        </template>

        <p style="text-align: center; font-size: 14px;line-height: 24px;width: 900px;margin-bottom: 12px;color: #999999;">{{ loadingtext }}..</p>

      </div>
    </div>

    <home-footer v-if="clientwidth>1080"></home-footer>

  </div>
</template>
<script>
// import lodash from 'lodash'
import {
  findBatchgetMaterial
  // findMaterial
} from '@/api/user.js'
import {
  getTime
} from '@/utils/moment.js'

import homeFooter from '@/components/home/footer.vue'
import homeHeader from '@/components/home/header.vue'
import headerPhone from '@/components/home/header-phone.vue'
export default {
  name: 'NewsList', // 路由名称
  components: {
    homeFooter,
    homeHeader,
    headerPhone
  },
  data() {
    return {
      clientwidth: window.innerWidth,
      loadingtext: '正在加载中',
      haderBg: require('@/assets/images/homepage/news-header-bg.svg'),
      activeIndex: 0,
      newsList: [], // 文章列表
      content: '',
      hTop: 0, // 文章列表盒子距离文档顶部的距离
      yTop: 0, // 文章列表盒子侧边栏定位的距离
      isPo: false
    }
  },
  computed: {
    listWidth() {
      if (this.newsList.length) {
        const width = (this.newsList.length + 1) * 6.2
        return width + 'rem'
      } else {
        return '1rem'
      }
    }
  },
  watch: {
    // obj: { //监听的对象
    //   deep: true, //深度监听设置为 true
    //   // 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
    //       immediate: true
    //   handler: function(newV, oldV) {
    //     console.log('watch中：', newV)
    //   }
    // }
  },
  // 创建后
  created() {
    this.findWXNewsList()
  },
  // 挂载后
  mounted() {
    window.addEventListener('resize', () => {
      this.clientwidth = window.innerWidth
    })
    // <meta name="referrer" content="never">
    // const metaInfo = document.createElement('meta')
    // metaInfo.setAttribute('name',
    //   'referrer')
    // metaInfo.setAttribute('content',
    //   'never')
    // metaInfo.src = 'https://hm.baidu.com/hm.js?d9040b2f7c03f685cb929f478aa32720'
    // document.head.prepend(metaInfo)
  },
  // 销毁后
  beforeDestroy() {
    window.removeEventListener('resize', () => {})
    // const metaList = document.getElementsByName('referrer')
    // metaList.forEach(item => {
    //   if (item.content === 'never') {
    //     document.head.removeChild(
    //       item)
    //   }
    // })
  },

  // 方法
  methods: {
    spliceColor(name) {
      let index = this.newsList.findIndex(item => {
        return item.name === name
      })
      index = index < 0 ? 0 : index - 1
      const arr = ['rgb(184, 123, 185)', 'rgb(97, 175, 200)', 'rgb(221, 195, 128)', 'rgb(162, 139, 228)',
        'rgb(221, 128, 128)', 'rgb(61, 83, 221)'
      ]
      return arr[index]
    },
    spliceName(name) {
      if (name !== '半月时谈' && name !== '增长日历' && name !== '推广干货') {
        return name.slice(0, 2)
      } else {
        return name.slice(2)
      }
    },
    // 获取微信文章列表
    async findWXNewsList() {
      const res = await findBatchgetMaterial()
      if (res.data.code === 20000) {
        // const arr = res.data.data.item
        const arr = [{ name: '最新动态', list: [] }]

        const result = res.data.data

        for (const key in result) {
          const index = arr.findIndex(item => {
            return key === item.name
          })

          const list = result[key].map(v => {
            const obj = {
              typeLabel: key,
              time: getTime({
                time: v.update_time * 1000,
                type: 'YYYY年MM月DD日HH:mm:ss'
              }),
              media_id: v.media_id,
              ...v.content.news_item[0]
            }
            return obj
          })
          arr[0].list.push(...list)
          if (index === -1) {
            arr.push({
              name: key,
              list
            })
          }
        }
        arr[0].list = arr[0].list.sort((a, b) => { return a.update_time - b.update_time })
        this.newsList = arr
        this.loadingtext = '没有更多了'
        return
      } else {
        this.loadingtext = '加载失败，请刷新重试'
      }
    },

    toDetails(mid) {
      const type = this.activeIndex
      this.$router.push({
        path: '/news-details',
        query: {
          mid,
          type

        }
      })
    },

    changeActiveIndex(index) {
      this.activeIndex = index
    }
  }
}
</script>

<style scoped lang="scss">
  @import '~@/styles/variables.scss';

  body section img {
    max-width: 100% !important;
    box-sizing: border-box !important;
    -webkit-box-sizing: border-box !important;
    word-wrap: break-word !important;
  }

  body {
    min-width: 1200px;
  }

  .news-list-layout {
    background-color: #f9f9f9 !important;

    .news-header {
      padding-top: 270px;
      width: 100vw;
      min-width: 1200px;
      height: 572px;
      margin-bottom: 24px;
      overflow: hidden;
      position: relative;
      background-repeat: no-repeat;
      background-size: cover;

      .mask {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 0;
        background-color: rgba($color: #000000, $alpha: 0.5);

      }

      p {
        position: relative;
        z-index: 1;
        margin: 0 auto;
        width: 1200px;
        text-align: left;
        color: #FFFFFF;
      }

      .title {
        font-size: 46px;
        line-height: 64px;
        margin-bottom: 48px;
      }

      .sub-title {
        font-size: 24px;
        line-height: 34px;
      }
    }

    .news-list-main {
      margin: 0 auto;
      width: 1200px;
      overflow: hidden;
      background-color: #f9f9f9 !important;
      align-items: flex-start;

      .main-slide {
        width: 148px;
        margin-right: 24px;
        margin-bottom: 24px;
        background-color: #FFFFFF;
        min-height: 384px;

        p {
          display: block;
          width: 148px;
          height: 64px;
          font-size: 16px;
          line-height: 64px;
          text-align: center;
          border-bottom: 1px solid rgba(198, 197, 197, 0.1);
          color: rgba(0, 0, 0, 0.6);

          i {
            margin-left: 24px;
            font-size: 14px;
            color: #ACB6C2;
            ;
          }

          &:last-of-type {
            border-bottom: none;
          }

          &.active {
            i {
              color: $mainColor1;
            }

            color: $mainColor1;
          }
        }
      }

      .list-box {
        flex: 1;
        min-width: 600px;

        .new-box {

          background-color: #FFFFFF;
          padding: 32px;
          margin-bottom: 24px;
          justify-content: flex-start;

          .new-type {
            display: none;
          }

          .new-img {
            margin-right: 48px;
            width: 244px;
            position: relative;
            margin-bottom: 8px;

            .img-top,
            .img-bg {
              width: 244px;
              height: 104px;
              position: relative;
              z-index: 1;
            }

            .img-bg {
              position: absolute;
              z-index: 0 !important;
              left: -8px;
              bottom: -8px;

              overflow: hidden;

              ::v-deep img.el-image__inner {
                opacity: 0.2;
                display: block;
                filter: blur(4px);
              }
            }
          }

          .new-info {
            .news-title {
              font-size: 20px;
              line-height: 20px;
              max-width: 588px;

            }

            .news-digest {
              margin: 24px 0;
              font-size: 14px;
              line-height: 14px;
            }

            .new-sec {
              width: 588px;

              p {
                font-size: 12px;
                color: #ACB6C2;
                display: flex;
                align-self: center;

                i {
                  font-size: 14px;
                  margin-right: 8px;
                  color: $mainColor1;
                }
              }
            }
          }
        }
      }
    }

  }

  .list-box::-webkit-scrollbar {
    width: 10px;
    height: 20px;
    background-color: #F5F5F5;
  }

  .list-box::-webkit-scrollbar-thumb {
    background-color: #e5e5e5;
    border-radius: 10px;
  }

  /* pc端小屏样式*/
  @media screen and (min-width: 1081px) and (max-width: 1240px) {
    .news-list-layout {
      .news-header {
        min-width: 100vw;

        p {
          width: 100vw;
          text-align: center;
        }
      }

      .news-list-main {
        width: 100vw;
        padding-right: 24px;
      }
    }

  }

  /* 移动端样式*/
  @media screen and (max-width: 1080px) {
    .news-list-layout {
      width: 100vw;
      min-width: 100vw;

      .news-header {
        width: 100vw;
        min-width: 100vw;
        background-position: center;
        height: 20.457rem;
        overflow: hidden;
        padding: 0 1.714rem;
        padding-top: 8.26rem;
        margin-bottom: 0;

        .title {
          font-weight: 600;
          font-size: 1.371rem;
          line-height: 2.286rem;
          width: 100%;
          text-align: center;
        }

        .sub-title {
          width: 100%;
          text-align: center;
          font-size: 0.8rem;
          line-height: 0.8rem;
        }
      }

      .news-list-main {
        width: 100vw;
        min-width: 100vw;
        padding-top: 1.371rem;
        flex-direction: column;
        background-color: #FFFFFF !important;
        min-height: auto;

        .main-slide {
          min-height: auto;
          width: 100vw;
          overflow-y: hidden;
          overflow-x: auto;
          border-left: 1.6rem solid #FFFFFF;
          margin-bottom: 0.2rem;
          scrollbar-width: none;
          -ms-overflow-style: none;

          &::-webkit-scrollbar {
            display: none;
            /* Chrome Safari */
          }

          li {
            display: inline-block;
            width: 5.943rem;
            height: 2.286rem;
            margin-right: 1.14rem;
            line-height: 2.286rem;
            text-align: center;
            border-radius: 4px;
            color: rgba(51, 64, 74, 0.8);
            background: rgba(198, 197, 197, 0.1);

            span {
              font-weight: 500;
              font-size: 0.8rem;
              line-height: 0.914rem;
            }

            i {
              display: none;
            }

            &.active {
              color: #FFFFFF;
              background: rgba(21, 197, 206, 1);
            }
          }

        }

        .list-box {
          width: 100vw;
          min-width: 100vw;
          padding: 1.34rem 0;
          overflow: hidden;

          .new-box {
            flex-direction: row-reverse;
            align-items: flex-start;
            justify-content: space-between;
            width: 100%;
            padding: 1.3rem 1.6rem;
            padding-top: 0;
            border-bottom: 1px solid rgba(232, 234, 237, 0.4);
            ;

            .new-type {
              display: block;
              width: 1em;
              word-wrap: break-word;
              font-size: 0.686rem;
              line-height: 0.971rem;
            }

            .new-info {
              width: 13.2rem;

              .news-title {

                font-weight: 500;
                font-size: 1.029rem;
                line-height: 1.143rem;
              }

              .news-digest {
                width: 12.4rem;
                margin-top: 0.4rem;
                font-size: 0.686rem;
                line-height: 1.029rem;
                margin-bottom: 1.029rem;
              }
            }

            .new-img {
              width: 2.457rem;
              height: 2.343rem;
              margin-right: 0;

              .img-top {
                width: 2.457rem;
                height: 2.343rem;
                margin-right: 0;
              }

              .img-bg {
                display: none;
              }
            }
          }
        }
      }
    }

  }
</style>
